/**
 * @desc 用户信息模块
 * @author menguang
 * @date 2024-04-20
 */

export default {
	namespaced: true,
	state: {
		userInfo: {
			token: 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE3Mjk4Njk0MTksInVzZXJuYW1lIjoi5LquMjQifQ.7kcDDAGIFf419bjNSpdvwLoDRTKhDyA6m69Qxu6e5n0',
			username: 'omth_5BG8ZnuUdwsOEVI05-xv5og',
			phone: '18056013469',
			userType: 1,
			companyId: '1816354091354415105',
			companyName: '',
			avatar: '',
		},
		isCompany: false // 是否是企业版，默认是个人版
	},
	getters: {
		token: state => state.userInfo.token || '',
		userName: state => state.userInfo.username || '',
		companyId: state => state.userInfo.companyId || '',
		companyName: state => state.userInfo.companyName || '',
		userType: state => state.userInfo.userType || 2,
		avatar: state => state.userInfo.avatar,
		isCompany: state => state.isCompany || false,

	},
	mutations: {
		SET_USER_INFO(state, payload) {
			state.userInfo = payload;
		},
		SET_COMPANY(state, payload) {
			state.isCompany = payload;
		}
	},
	actions: {
		loginInfo({ commit }, payload) {
			commit('SET_USER_INFO', payload);
		},
		// update 时忽略 token 数据
		updateUserInfo({ commit, getters }, payload) {
			const token = getters.token;
			const data = { ...payload, token };
			commit('SET_USER_INFO', data);
		},
		// 设置是否是企业版,只需传入 true or false
		setCompany({ commit }, payload) {
			commit('SET_COMPANY', payload);
		},
		logout({ commit }) {
			commit('SET_USER_INFO', {});
		}
	}
};

